//
//  EAMyTable.m
//  WhatToEat
//
//  Created by Mac on 15/3/26.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "EAMyTable.h"
#import "EARecommendMealCell.h"
#import "EAScoreViewController.h"

@interface EAMyTable ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UIView * dinnerLine;
@property(strong,nonatomic)UITableView * tableView;
@property(strong,nonatomic)NSDictionary * dict;
@property(strong,nonatomic)UIButton * breakfastBtn;
@property(strong,nonatomic)UIButton * lunchBtn;
@property(strong,nonatomic)UIButton *dinnerBtn;
@property(strong,nonatomic)UIView * noDataView;
@property(strong,nonatomic)UIButton * toSelectBtn;
//@property(strong,nonatomic)NSDictionary * dict;
@end

@implementation EAMyTable

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateUI];
}

-(void)updateUI
{
    
    
    
    self.navigationItem.leftBarButtonItem = nil;

    self.navigationItem.title=@"我的餐桌";
    UIButton * breakfastBtn=[[UIButton alloc] init];
    [breakfastBtn setTitleColor:kTextLightColor forState:0];
    [breakfastBtn setTitle:@"早餐" forState:0];
    [self.view addSubview:breakfastBtn];
    
    [breakfastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/3, 34));
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        
        
    }];
    self.breakfastBtn=breakfastBtn;
    
    breakfastBtn.tag=1;
    
    [breakfastBtn addTarget:self action:@selector(changMealAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton * lunchBtn=[[UIButton alloc] init];
    [lunchBtn setTitleColor:kTextLightColor forState:0];
    [lunchBtn setTitle:@"午餐" forState:0];
    [self.view addSubview:lunchBtn];
    lunchBtn.tag=2;
    [lunchBtn addTarget:self action:@selector(changMealAction:) forControlEvents:UIControlEventTouchUpInside];
    [lunchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/3, 34));
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(breakfastBtn.mas_right);
        
        
    }];
    self.lunchBtn=lunchBtn;
    
    UIButton * dinnerBtn=[[UIButton alloc] init];
    [dinnerBtn setTitleColor:kTextLightColor forState:0];
    [dinnerBtn setTitle:@"晚餐" forState:0];
    [self.view addSubview:dinnerBtn];
    self.dinnerBtn=dinnerBtn;
    dinnerBtn.tag=3;
    [dinnerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/3, 34));
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(lunchBtn.mas_right);
        
        
    }];
        [dinnerBtn addTarget:self action:@selector(changMealAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView * lineView=[[UIView alloc] init];

    lineView.backgroundColor=kLineColor;
    [self.view addSubview:lineView];

    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH, 2));
        
        
        make.centerX.mas_equalTo(self.view);
        
        make.top.mas_equalTo(breakfastBtn.mas_bottom).with.offset(0);
        
        
    }];
    
    

    self.dinnerLine=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH/3, 2)];
    
    self.dinnerLine.backgroundColor=kTingColor;
    
    [lineView addSubview:self.dinnerLine];
    

    
    
    
    self.tableView=[[UITableView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate=self;
    
    self.tableView.dataSource=self;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.mas_equalTo(lineView.mas_bottom);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH, kUI_SCREEN_HEIGHT-46-66-20));
        
    }];
    
    

    
    [self.tableView registerNib:[UINib nibWithNibName:@"EARecommendMealCell" bundle:nil] forCellReuseIdentifier:@"EARecommendMealCell"];
    
    [self.tableView reloadData];
    
    
    
    self.noDataView=[self noDataViews];

    self.noDataView.backgroundColor=[UIColor redColor];
    
    NSLog(@"%@",self.noDataView.backgroundColor);
    
    self.noDataView.userInteractionEnabled=YES;
    
    self.noDataView.hidden=YES;
    
    
    [Commtools setExtraCellLineHidden:self.tableView];
}

-(void)updateData:(int)i
{

    [WebAPI webAPISignAddMd5tWithNote:@"user/gettable" parm:@{
                                                              @"userID":ApplicationDelegate.userId
                                                             // @"addTime":@"2016-01-20"
                                                              } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
        
     
                                                                  if ([kStr(responseObject[@"code"]) isEqualToString:@"1"]) {
                                                                      
                                                                      
                                                                      self.dict=responseObject;
                                                                      
                                                                      if (i==0) {
                                                                          
                                                                
                                                                      
                                                                      
                                                                      if ([self.dict[@"m"] isKindOfClass:[NSDictionary class]]) {
                                                                          
                                                                          
                                                                          [self changMealAction:self.breakfastBtn];
                                                                          
                                                                          
                                                                      }
                                                                      
                                                                      if ([self.dict[@"a"] isKindOfClass:[NSDictionary class]]) {
                                                                          
                                                                          [self changMealAction:self.lunchBtn];
                                                                          
                                                                          
                                                                          
                                                                      }
                                                                      if ([self.dict[@"e"] isKindOfClass:[NSDictionary class]]) {
                                                                          
                                                                          
                                                                          
                                                                          [self changMealAction:self.dinnerBtn];
                                                                          
                                                                      }
                                                                      
                                                                      
                                                                      
                                                                     [self changMealAction:self.breakfastBtn];
                                                                      }else
                                                                      {
                                                                      
                                                                      
                                                                          if (i==1) {
                                                                              
                                                                              
                                                                              [self changMealAction:self.breakfastBtn];
                                                                              
                                                                              
                                                                          }
                                                                          
                                                                          if (i==2) {
                                                                              
                                                                              [self changMealAction:self.lunchBtn];
                                                                              
                                                                              
                                                                              
                                                                          }
                                                                          if (i==3) {
                                                                              
                                                                              
                                                                              
                                                                              [self changMealAction:self.dinnerBtn];
                                                                              
                                                                          }
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      }
                                                                      
                                                                      [self.tableView reloadData];
                                                             
                                                                      
                                                                  }
                                                                  
                                                                  
                                                                  
        
    }];
    

}

//#pragma mark------赋值
//-(void)setDict:(NSDictionary *)dict
//{
//    _dict=dict;
//    
//    if ([dict[@"m"] isKindOfClass:[NSDictionary class]]) {
//        
//        
//        self.foods=dict[@"m"][@"meal"];
//        
//        
//    
//        
//        
//    }else
//    {
//    //早餐为空
//    
//    
//    }
//    
//    if ([dict[@"a"] isKindOfClass:[NSDictionary class]]) {
//        
//           self.foods=dict[@"m"][@"meal"];
//        
//        
//        
//    }
//    
//    
//    
//    
//        [self.tableView reloadData];
//
//}


-(void)setDict:(NSDictionary *)dict
{

    _dict=dict;
    
    




}




-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    [self updateData:0];
   
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateActions:) name:updateInMyTable object:nil];
    
    

}

- (void)viewWillDisappear:(BOOL)animated
{
 
    [super viewWillDisappear:animated];
    
   [ [NSNotificationCenter defaultCenter] removeObserver:self
name:updateInMyTable
object:nil];
}

-(void)updateActions:(NSNotification *)send
{


    
    [self updateData:kStr(send.object).intValue];

    
    
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    


}


-(void)changMealAction:(UIButton * )bt
{


    [UIView animateWithDuration:0.3 animations:^{
       
        self.dinnerLine.left=bt.left;
        
        
    }];
    
    
    bt.selected=YES;
 

    
    for (UIView * v in self.view.subviews) {
        
        if ([v isKindOfClass:[UIButton class]]) {
            
            UIButton * bt1=(UIButton *)v;
            
            
            if ([bt1 isEqual:bt]) {
                
                bt.selected=YES;
                
            }else
            {
            
                bt1.selected=NO;
            }
            
            
            
            
        }
        
        
        
    }
    
    

    
    switch (bt.tag) {
        case 1:
            
            
                if ([self.dict[@"m"] isKindOfClass:[NSDictionary class]]) {
            
            
                    self.foods=self.dict[@"m"][@"meal"];
            
            
                    self.noDataView.hidden=YES;
                    self.toSelectBtn.hidden=YES;
                    
                    
                }else
                {
                //早餐为空
                    self.foods=[@[] mutableCopy];
                    
                  
                    self.noDataView.hidden=NO;
                    self.toSelectBtn.hidden=NO;
                
                }
            
   
            

            break;
            
        case 2:
            
            
            if ([self.dict[@"a"] isKindOfClass:[NSDictionary class]]) {
                
                
                self.foods=self.dict[@"a"][@"meal"];
              
                
        
                
                self.noDataView.hidden=YES;
                self.toSelectBtn.hidden=YES;
            }else
            {
                //中餐为空
                   self.foods=[@[] mutableCopy];
                
                self.noDataView.hidden=NO;
                self.toSelectBtn.hidden=NO;
            }

            break;
            
        case 3:
            if ([self.dict[@"e"] isKindOfClass:[NSDictionary class]]) {
                
                
                self.foods=self.dict[@"e"][@"meal"];
                
                self.noDataView.hidden=YES;
                
                self.toSelectBtn.hidden=YES;
                
            }else
            {
                //中餐为空
                self.foods=[@[] mutableCopy];
                self.noDataView.hidden=NO;
                self.toSelectBtn.hidden=NO;
            }

            break;
            
        default:
            break;
    }
    
    

    
    [self.tableView reloadData];
    


    
    
}




#pragma mark-------无数据


-(UIView *)noDataViews
{

    UIView * v=[[UIView alloc] initWithFrame:self.tableView.bounds];
    v.backgroundColor=[UIColor redColor];
    [self.view addSubview:v];

    UIImageView * iv=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wu"]];
    
    iv.frame=CGRectMake(44, 37, 220, 180);
    iv.contentMode=UIViewContentModeScaleAspectFit;
    
    iv.center=CGPointMake(kUI_SCREEN_WIDTH/2, 50+iv.height/2);
    [v addSubview:iv];
    v.userInteractionEnabled=YES;
    
    UILabel * l=[UILabel new];
    l.text=@"餐桌上空空的，什么都没有";
    l.textAlignment=1;
    l.font=kFont(14);
    l.frame=CGRectMake(kUI_SCREEN_WIDTH/2-125, 240, 250, 28);
    [v addSubview:l];
    l.textColor=kLightGrayColor;
    l.top=iv.bottom+33;
    

    UIButton * bt=[UIButton buttonWithType:0];
    [self.tableView addSubview:bt];
    [bt setBackgroundColor:kWhiteColor];
    bt.frame=CGRectMake(kUI_SCREEN_WIDTH/2-150/2, 282, 150, 30);
    bt.titleLabel.font=kFont(15);
    [bt setTitle:@"去选餐" forState:UIControlStateNormal];
    [bt setTitleColor:kTingColor forState:UIControlStateNormal];
    bt.layer.borderColor=kTingColor.CGColor;
    bt.layer.borderWidth=1;
    bt.top=l.bottom+53;
    bt.userInteractionEnabled=YES;
    [bt addTarget:self action:@selector(toChooseMeal) forControlEvents:UIControlEventTouchUpInside];
    bt.enabled=YES;
    self.toSelectBtn=bt;
    
    UITapGestureRecognizer *t=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toChooseMeal)];
    
    [v addGestureRecognizer:t];
    
    NSLog(@"%d",bt.userInteractionEnabled);
    
    return v;
    


}


-(void)toChooseMeal
{


    UITabBarController * tVC=self.tabBarController;
    
    EATabBarController * eTB=(EATabBarController *)tVC;
    
    
    [eTB.customTabBarView buttonWasSelected:0];
    
    
    tVC.selectedIndex=0;

}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (self.foods.count==0) {
        
        return 0;
    }
    

    return 150;
    
    

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    EARecommendMealCell * cell = [tableView dequeueReusableCellWithIdentifier:@"EARecommendMealCell" forIndexPath:indexPath];
    
    NSDictionary * d=self.foods[indexPath.row];
    
    
    
    cell.nameLabel.text=d[@"name"];
    
    
    
    [cell.headImageView setImageWithURL:[NSURL URLWithString:d[@"img"]]];
    
    
//    cell.numberLabel.text=@"X7";
//    cell.yuyangLabel.text=[NSString stringWithFormat:@"营养元素:%@",d[@"nutrition"]];
//    cell.kllLabel.text=[NSString stringWithFormat:@"卡路里:%@",d[@"calory"]];
    
    
    cell.yuyangLabel.text=[NSString stringWithFormat:@"营养元素:%@",d[@"nutrition"]];
    cell.kllLabel.text=[NSString stringWithFormat:@"卡路里:%@",d[@"calory"]];
    cell.num=[NSString stringWithFormat:@"%@",d[@"num"]].intValue;

    
    
    cell.minusBlock=^(NSString * type){
        

        
    };
    
    
    
    
    
    cell.addBtn.hidden=YES;
    
    cell.foodsBtn.hidden=YES;
    
    
    
    return cell;
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.foods.count;
    
}

//
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    
    
    NSString * secore;
    
    if (self.dinnerBtn.selected==YES) {
        
        secore=self.dict[@"e"][@"totalcaly"];
        
        
    }
    
    if (self.lunchBtn.selected==YES) {
        
        secore=self.dict[@"a"][@"totalcaly"];
    }
    
    
    if (self.breakfastBtn.selected==YES) {
        secore=self.dict[@"m"][@"totalcaly"];
    }
    
    
    
    
    UIView * v=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, 150)];
    
    v.backgroundColor=[UIColor whiteColor];

    
    
    UIView * line1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, 1)];
    
    line1.backgroundColor=kLineColor;
    
    [v addSubview:line1];
    
    
    
    UILabel * tipLabel=[[UILabel alloc] initWithFrame:CGRectMake(22, 5, kUI_SCREEN_WIDTH-50, 40)];
    tipLabel.text=@"吃神马温馨提示:";

    [v addSubview:tipLabel];
    
    
    UILabel * tipLabels=[[UILabel alloc] initWithFrame:CGRectMake(22, tipLabel.bottom, kUI_SCREEN_WIDTH-50, 22)];
    tipLabels.text=secore;
    tipLabels.font=kFont(13);
    
    [v addSubview:tipLabels];
    
    UIView * line2=[[UIView alloc] initWithFrame:CGRectMake(20, tipLabels.bottom+10, kUI_SCREEN_WIDTH-40, 1)];
    
    line2.backgroundColor=kLineColor;
    
    [v addSubview:line2];
    
    
    EAMButton * bt=[[EAMButton alloc] initWithFrame:CGRectMake(20,60, kUI_SCREEN_WIDTH/2, 36)];
    
    [bt setTitle:@"查看膳食评分" forState:0];
    
    
    bt.center=v.center;
    
    bt.top=bt.top+40;
    
    [v addSubview:bt];
    
    
    [bt addTarget:self action:@selector(toScore) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    
    
    
    return v;
    
    
    
}

-(void)toScore
{

    EAScoreViewController * sVc=[[EAScoreViewController alloc] init];
    
    NSString * secore;
    
    if (self.dinnerBtn.selected==YES) {
        
        secore=self.dict[@"e"][@"score"];
        
        
    }
    
    if (self.lunchBtn.selected==YES) {
        
        secore=self.dict[@"a"][@"score"];
    }
    
    
    if (self.breakfastBtn.selected==YES) {
        secore=self.dict[@"m"][@"score"];
    }
    
    
    sVc.scores=secore;
    
    [self.navigationController pushViewController:sVc animated:YES];


}

@end
